﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Linq;
using EBAYVN.DOMAIN;
using EBAYVN.Util;


namespace EBAYVN.DAO
{
    public  class NhanVienDao
    {
        private readonly EBAYVNDOMAINDataContext _db;
        public static readonly string Connectionstring = ConfigurationManager.AppSettings[Constants.SETTINGS_CONNECTION];
        public NhanVienDao()
        {
            _db = new EBAYVNDOMAINDataContext(Connectionstring);
        }
        
        public NHANVIEN    Get(string ma)
        {
            return _db.NHANVIENs.Where(p => p.MANV.Equals(ma)).SingleOrDefault();
        }

        

        public List<NHANVIEN> Search(string key)
        {
            var query = _db.NHANVIENs.AsQueryable();
            return query.Where(p => p.HOTEN.ToUpper().Contains(key.ToUpper())
                || p.MANV.ToUpper().Contains(key.ToUpper()))
                    .OrderBy(p => p.HOTEN)
                    .ToList();
        }
        public List<NHANVIEN> Search(string key,string macv)
        {
            var query = _db.NHANVIENs.AsQueryable();
            query = query.Where(p => ( p.HOTEN.ToUpper().Contains(key.ToUpper()))
                || p.MANV.ToUpper().Contains(key.ToUpper()));
            return query.OrderBy(p => p.HOTEN).ToList();
        }
        public List<NHANVIEN> GetListNhanVienPhanCongLichTrinh(string key, string macv)
        {
            var query = _db.NHANVIENs.AsQueryable();
            query = query.Where(p => ( p.HOTEN.ToUpper().Contains(key.ToUpper()))
                || p.MANV.ToUpper().Contains(key.ToUpper()));
            return query.OrderBy(p => p.HOTEN).ToList();
        }

        public List<NHANVIEN> GetList()
        {
            return _db.NHANVIENs.ToList();
        }

        public List<NHANVIEN> GetList(String manv, String tennv, String congviec)
        {
            var query = _db.NHANVIENs.AsEnumerable();

            if (!String.IsNullOrEmpty(manv))
                query = query.Where(nv => nv.MANV.ToUpper().Contains(manv.ToUpper()));

            if (!String.IsNullOrEmpty(tennv))
                query = query.Where(nv => nv.HOTEN.ToUpper().Contains(tennv.ToUpper()));
            //if (!String.IsNullOrEmpty(congviec) && congviec != "%")
            //    query = query.Where(nv => nv.MACV.ToUpper().Equals(congviec.ToUpper()));

            return query.ToList();
        }


        public List<NHANVIEN> GetListByCV(string maCv)
        {
            return _db.NHANVIENs.ToList();
        }

        
        public List<NHANVIEN> GetList(int fromIndex, int toIndex)
        {
            return GetList().Skip(fromIndex).Take(toIndex - fromIndex).ToList();
        }

        public int Count()
        {
            return _db.NHANVIENs.Count();
        }

        public Message Insert(NHANVIEN objUi)
        {
            Message msg;
            try
            {

                _db.Connection.Open();

                _db.NHANVIENs.InsertOnSubmit(objUi);
                _db.SubmitChanges();
                msg = new Message(MessageConstants.I_CREATE_SUCCEED, MessageType.Info, "nhân viên");
            }
            catch (Exception ex)
            {
                msg = ExceptionHandler.HandleInsertException(ex, "nhân viên");
            }
            return msg;
        }

        public Message Update(NHANVIEN objUi)
        {
            Message msg;
            try
            {
                var objDb = Get(objUi.MANV  );

                if (objDb != null)
                {
                    
                    objDb.HOTEN = objUi.HOTEN;
                    objDb.CMND = objUi.CMND;
                    objDb.NGAYSINH = objUi.NGAYSINH;
                    objDb.DIACHI = objUi.DIACHI;
                    objDb.DIENTHOAI = objUi.DIENTHOAI;
                    objDb.EMAIL = objUi.EMAIL;
                    _db.SubmitChanges();

                     
                    msg = new Message(MessageConstants.I_UPDATE_SUCCEED, MessageType.Info, "nhân viên");
                }
                else
                {
                    msg = new Message(MessageConstants.E_OBJECT_IN_USED, MessageType.Error, "nhân viên");
                }
            }
            catch (Exception ex)
            {
                msg = ExceptionHandler.HandleUpdateException(ex, "Nhân viên ", objUi.HOTEN    );
            }
            return msg;
        }

        public bool IsInUse(string ma)
        {
            
                return true;
            
        }

        public Message Delete(NHANVIEN objUi)
        {
            Message msg;

            try
            {
                 
                var objDb = Get(objUi.MANV );

                if (objDb == null)
                {
                   
                    msg = new Message(MessageConstants.E_OBJECT_NOT_EXISTS, MessageType.Error, "Nhân viên ", objUi.MANV );
                    return msg;
                }

                
                _db.NHANVIENs.DeleteOnSubmit(objDb);
                _db.SubmitChanges();
                msg = new Message(MessageConstants.I_DELETE_SUCCEED, MessageType.Info, "Nhân viên ");
            }
            catch (Exception ex)
            {
                msg = ExceptionHandler.HandleDeleteException(ex, "Nhân viên ");
            }

            return msg;
        }

        // <summary>
        // Delete list
        // </summary>
        // <param name="objList"></param>
        // <param name="action"></param>
        // <returns></returns>
        public Message DeleteList(List<NHANVIEN> objList, PageAction action)
        {
            Message msg;
            DbTransaction trans = null;
            try
            {
                _db.Connection.Open();
                trans = _db.Connection.BeginTransaction();
                _db.Transaction = trans;

                foreach (var obj in objList)
                {
                    Delete(obj);
                }
                trans.Commit();
                msg = new Message(MessageConstants.I_DELETE_SUCCEED, MessageType.Info, "Nhân viên ");
            }
            catch (Exception ex)
            {
                if (trans != null)
                    trans.Rollback();

                msg = ExceptionHandler.HandleInsertException(ex, "Danh sách nhân viên ");
            }

            return msg;
        }
    }
}
